package com.baseDemo.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baseDemo.web.entity.QuartzEntity;
import com.baseDemo.web.entity.UserEntity;
import com.baseDemo.web.service.quartz.QuartzService;
import com.baseDemo.web.service.user.UserService;
import com.share.MD5Util;

@Controller
@RequestMapping("/base")
public class BaseController {
	private Logger logger = Logger.getLogger(getClass());
	@Resource
	private UserService userService;
	@Resource private QuartzService quartzService;
	
	@RequestMapping("/ch")
	public String ch() {
/*	User o = new User();
		o.setAge("2");
		o.setUserName("asd");
		userService.save(o);
		//userService.addUser(o);
		System.out.println("save");*/
		//userService.quartzTest();
/*		QuartzEntity entity = new QuartzEntity();
		entity.setJobName("quartzT");
		entity.setGroupName("group_1");
		entity.setClazzName("com.baseDemo.web.quartz.test.QuartzT");
		entity.setCronExpression("0/5 0/1 * * * ?");
		entity.setCronMethod("execute1");
		//quartzService.addAndStartQuartz(entity);
*/			UserEntity userEntity = new UserEntity();
			userEntity.setUserName("管理员");
			userEntity.setLoginID("admin");
			userEntity.setPassword(MD5Util.getMD5String("admin"));
			userEntity.setActive(true);
			userService.save(userEntity);
		return "ch";
	}
	@RequestMapping("/adduser")
	public void adduser(){
/*		User o = new User();
		o.setAge("2");
		o.setUserName("asd");
		//userService.save(o);
		//userService.addUser(o);
		System.out.println("save");*/
	}
	@RequestMapping(value="/getNav",produces="text/plain;charset=UTF-8") 
	@ResponseBody
	public String getNav(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String nav = " {id:-1,leaf:false,text:'root',children:[ "+ 
				"   { "+ 
				"  id:'quartzModule',	 "+ 
				"   text: '定时任务管理', "+ 
				"   expanded: true, "+ 
				"  leaf:false, "+ 
				" children: [ "+ 
				"      { id:'quartzCron', controller:'quartzCron.QuartzCronController',clazz:'quartzCron-grid', icon : 'theme/images/user.png',text: 'Quartz管理', leaf: true }, "+ 
				"      { id:'multisort',  controller:'dataview.MultiSortController',clazz:'dataview-multisort', icon : 'theme/images/user.png',text: '动画', leaf: true }, "+ 
				"      { id:'login', clazz:'login-forms', text: 'login-form', leaf: true } "+ 
				"  ] "+ 
				"  } "+ 
				" ]}";
		/*response.setCharacterEncoding("utf-8");
		response.getWriter().write(nav);*/
		return nav;
	}
	@RequestMapping("/getQuartz")
	public void getQuartz(@RequestParam("page") Integer page,@RequestParam("limit") Integer limit,@RequestParam("start") Integer start,HttpServletResponse response) throws IOException{
		logger.info(page);
		logger.info(limit);
		logger.info(start);
		String s = "{total:19,quartz:[{id:'aaaa',jobName:23,groupName:'527231626@qq.com',clazzName:'aaaa',cronExpression:'cronExpression',cronMethod:'asd'}]}";
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(s);
	}
	@RequestMapping(value="/validataUser.json")
	@ResponseBody
	public Map<String,Object> validataUser(@RequestParam String userName){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("code", true);
		return map;
	}
	
}
